<template>
  <div class="app-container">
    <el-form ref="queryForm" inline :model="queryParams">
      <el-form-item prop="appName">
        <el-input v-model="queryParams.appName" placeholder="应用名称" />
      </el-form-item>
      <el-form-item prop="deviceId">
        <el-input v-model="queryParams.deviceId" placeholder="设备标识" />
      </el-form-item>
      <el-form-item prop="begin">
        <el-date-picker
          v-model="queryParams.begin"
          type="datetime"
          clearable
          placeholder="访问开始时间"
          default-time="00:00:00"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item prop="end">
        <el-date-picker
          v-model="queryParams.end"
          type="datetime"
          clearable
          placeholder="访问结束时间"
          default-time="23:59:59"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item prop="dstIp">
        <el-input v-model="queryParams.dstIp" placeholder="访问IP" />
      </el-form-item>
      <el-form-item prop="dstPort">
        <el-input v-model="queryParams.dstPort" placeholder="访问端口" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 查询 </el-button>
        <el-button type="primary" @click="resetQuery"> 重置 </el-button>
        <el-button
          type="primary"
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['/eva/v1/warning/export']"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="pageList" fit stripe height="615px">
      <el-table-column
        label="序号"
        type="index"
        width="80"
        align="left"
      ></el-table-column>
      <el-table-column
        label="应用名称"
        prop="appName"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="应用唯一标识"
        prop="appId"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="设备标识"
        prop="deviceId"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="设备备注"
        prop="memo"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column label="访问时间" width="190" align="left">
        <template slot-scope="scope">
          {{ scope.row.createdAt | parseTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="访问地址"
        prop="dstIp"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="告警等级"
        prop="level"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="备注"
        prop="memo"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column label="操作" align="left" width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['/eva/v1/warning/delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getWarningList, deleteWarning } from "@/api/warning";

export default {
  name: "Accesswarn",
  data() {
    return {
      queryParams: {
        page: 1,
        size: 10,
        appName: undefined,
        deviceId: undefined,
        begin: undefined,
        end: undefined,
        dstIp: undefined,
        dstPort: undefined,
      },
      loading: false,
      pageList: [],
      total: 0,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      this.downloadByGet(
        "/eva/v1/warning/export",
        this.queryParams,
        `告警记录_${new Date().getTime()}.xlsx`
      );
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getWarningList()
        .then((res) => {
          this.pageList = res.data.items || [];
          this.total = res.data.total || 0;
        })
        .finally(() => (this.loading = false));
    },
    handleDelete(row) {
      this.$confirm("确定要删除此告警记录吗？", "提示", {
        type: "warning",
      }).then(() => {
        deleteWarning({ id: row.id }).then((res) => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: "操作成功",
            });
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      });
    },
  },
};
</script>

<style></style>
